/**
 * 查询结果js
 * Created by mhy on 2017/1/9.
 */
$(document).ready(function () {

    //初始化表格
    function initTable(data){
        var $datatable = $('#datatable-checkbox');
        $datatable.dataTable({
            data:data,
            "createdRow": function ( row, data, index ) {
                $('td', row).css("text-align","center").css("vertical-align","middle");
            },
            columns:[
                {"sTitle": "发送时间"},
                {"sTitle": "公文标题"},
                {"sTitle": "公文号"},
                {"sTitle": "公文文种"},     //3
                {"sTitle": "紧急程度"},
                {"sTitle": "公文状态"}
            ],
            order:[
                [0, "desc"]
            ],
            columnDefs: [
                { orderable: false, targets: [0] }
            ],
            deferRender: true,
            paging:false
        } );

    }

    var $datatable = $('#datatable-checkbox');
    //获取url中的参数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");     //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);        //匹配目标参数
        if (r != null) return decodeURI(r[2]); return null;         //返回参数值
    }
    var $title = getUrlParam("title");                // 公文标题
    var $number = getUrlParam("number");                // 公文文号
    var $startTime = getUrlParam("startTime");      // 开始时间
    var $endTime = getUrlParam("endTime");          // 结束时间
    var $keywords = getUrlParam("keywords");        // 主题词

    $.ajax({
        type: 'POST',//请求类型
        url: '../document/list/findbox',//请求地址'
        contentType: "application/json",
        dataType: 'JSON',
        data:JSON.stringify({}),
        data: JSON.stringify({
            "title":$title+"",
            "keywords":$keywords+"",
            "number":$number+"",
            "starTime":$startTime,
            "endTime":$endTime
        }),
        success: function (json,status) {
            var datas = new Array();
            console.log(json.data);
            if(json.data!=null){
                var arr = new Array();
                var docs = json.data.documents;
                for(var i = 0;i<docs.length;i++){
                    arr = [];
                    arr[0] = docs[i].sent_time;
                    arr[1] = docs[i].title;
                    arr[2] = docs[i].number;
                    arr[3] = docs[i].type;
                    arr[4] = docs[i].serious_type;
                    if(docs[i].serious_type == "NOT_SERIOUS"){
                        arr[4] = "不紧急";
                    }else if(docs[i].serious_type == "SERIOUS"){
                        arr[4] = "紧急";
                    }
                    arr[5] = "<a href='#' class='btn btn-info' onclick='docContent(this,"+ docs[i].id + ")'>查看公文 </a>" + "<br/>";
                    var $approval = docs[i].approval;
                    if($approval == 1){
                        arr[5] +=  "阅读数量: " + docs[i].reader_count + "/" + docs[i].receiver_count;
                    }else if($approval == 0){
                        arr[5] += "<button type='button' class='btn btn-primary' onclick='docSent("+ docs[i].id +")'>发送公文</button>";
                        arr[5] += "<br/>公文未发送";
                    }
                    datas[i] = arr;
                }
            }
            initTable(datas);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(textStatus);
        }
    });
});

// 公文查看
function docContent(e,doc_id){
    var url = "../query/query_content.html?doc_id=" + doc_id;
    window.location.href = url;
}


// 公文发送
function docSent(doc_id) {
    if(confirm("是否发送该条公文?")){
        $.ajax({
            type: 'POST',
            url: '../document/sent/direct/' + doc_id,
            contentType: "application/json",
            dataType: 'JSON',
            success: function (json,status) {
                if(json.err_msg == null){
                    window.location.href = "../document/my_sent_doc.html";
                }else{
                    alert(json.err_msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(errorThrown);
            },
        })
    }
}
